package cn.qn.service.impl.apply;

import cn.qn.base.PageVO;
import cn.qn.dto.apply.ChargerBuildApplyDTO;
import cn.qn.dto.apply.ChargerBuildApplyQueryDTO;
import cn.qn.dto.apply.ChargerBuildApplySendDTO;
import cn.qn.enums.ChargerBuildApplyStatusEnum;
import cn.qn.exceptions.BizException;
import cn.qn.mapper.apply.ChargerBuildApplyMapper;
import cn.qn.moudle.apply.ChargerBuildApply;
import cn.qn.repository.apply.ChargerBuildApplyRepository;
import cn.qn.service.apply.ChargerBuildApplyService;
import cn.qn.utils.DateUtils;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author: Bryant
 * @date: 2019/5/31
 * @description:
 */
@Service
@com.alibaba.dubbo.config.annotation.Service(interfaceClass = ChargerBuildApplyService.class)
public class ChargerBuildApplyServiceImpl implements ChargerBuildApplyService {

    @Resource
    private ChargerBuildApplyMapper chargerBuildApplyMapper;

    @Autowired
    private ChargerBuildApplyRepository chargerBuildApplyRepository;
    @Override
    public PageVO<ChargerBuildApplyDTO> pageQueryChargerApply(ChargerBuildApplyQueryDTO query) {
        PageBounds pb = new PageBounds(query.getPageBounds().getPage(), query.getPageBounds().getLimit());
        List<ChargerBuildApplyDTO> list = chargerBuildApplyMapper.pageQueryChargerApply(query, pb);
        Integer count = chargerBuildApplyMapper.pageQueryChargerApplyCount(query);
        return PageVO.convert(pb.getPage(), pb.getLimit(), list, count);
    }

    @Override
    public ChargerBuildApplyDTO queryDetailById(Integer id) {
        return null;
    }

    @Override
    public void handle(ChargerBuildApplyDTO dto) throws BizException{
        ChargerBuildApply apply=chargerBuildApplyRepository.findChargerBuildApplyById(dto.getId());
        if(apply==null){
            throw new BizException("该记录不存在");
        }
        apply.setUpdateBy(dto.getUpdateBy());
        apply.setUpdateTime(new Date());
        apply.setRemark(dto.getRemark());
        apply.setStatus(dto.getStatus());
        chargerBuildApplyRepository.save(apply);
    }

    @Override
    public void saveApply(ChargerBuildApplySendDTO dto) throws BizException{
        ChargerBuildApply apply=new ChargerBuildApply();
        BeanUtils.copyProperties(dto,apply);
        apply.setAppointDate(DateUtils.parseStrToDate(dto.getAppointDate(),DateUtils.DATE_FORMAT_YY_MM_DD));
        apply.setCreateTime(new Date());
        apply.setStatus(ChargerBuildApplyStatusEnum.UN_CONTACTED.getCode());
        chargerBuildApplyRepository.save(apply);
    }
}
